package com.wicket.okrbff.common.ro;

import java.io.Serializable;


/**
 * @author ：CHS
 * @since 2022/3/1 15:08
 *
 *
 * @version 1.0
 */
public class OkrResult<T extends Serializable> implements Serializable {
    private static final long serialVersionUID = 1L;
    private String errCode;
    private String message;
    private T data;

    public OkrResult() {
        this.errCode = "1";
    }

    public OkrResult(String code, String message) {
        this(code, message, null, null);
    }

    public OkrResult(String code, String message, T data) {
        this(code, message, data, null);
    }

    public OkrResult(String code, String message, T data, Throwable cause) {
        this.errCode = code;
        this.message = message;
        this.data = data;
    }

    public static <T extends Serializable> OkrResult<T> success(String code, String message, T data) {
        OkrResult rest = new OkrResult();
        rest.setErrCode(code);
        rest.setMessage(message);
        rest.setData(data);
        return rest;
    }

    public static <T extends Serializable> OkrResult<T> success(T data) {
        OkrResult rest = new OkrResult();
        rest.setErrCode("1");
        rest.setMessage("");
        rest.setData(data);
        return rest;
    }

    public static <T extends Serializable> OkrResult<T> fail(String code, String message) {
        OkrResult<T> rest = new OkrResult();
        rest.setErrCode(code);
        rest.setMessage(message);
        return rest;
    }

    public static <T extends Serializable> OkrResult<T> fail(String code,T data ,String message) {
        OkrResult<T> rest = new OkrResult();
        rest.setErrCode(code);
        rest.setData(data);
        rest.setMessage(message);
        return rest;
    }

    public String toLog() {
        int length = 20;
        String data_str = null;
        if (this.errCode != null) {
            length += this.errCode.length();
        }

        if (this.message != null) {
            length += this.message.length();
        }

        if (this.data != null) {
            if (this.data instanceof OkrObject) {
                data_str = ((OkrObject) this.data).toLog();
                length += data_str.length();
            } else {
                data_str = this.data.toString();
                length += data_str.length();
            }
        }

        StringBuilder sb = new StringBuilder(length);
        sb.append("code:");
        sb.append(this.errCode);
        sb.append(" message:");
        sb.append(this.message);
        sb.append(" data:");
        sb.append(data_str);
        return sb.toString();
    }

    public boolean isSuccess() {
        Integer code = Integer.valueOf(this.errCode);
        return code > 0;
    }

    public String getErrCode() {
        return errCode;
    }

    public void setErrCode(String errCode) {
        this.errCode = errCode;
    }

    public String getMessage() {
        return this.message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public T getData() {
        return this.data;
    }

    public void setData(T data) {
        this.data = data;
    }
}
